﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Net.Mail;

namespace TDLib.Net
{
    /// <summary>
    /// 邮件发送类
    /// </summary>
    public class EmailSender
    {
        string m_senderMail = "";
        string m_senderUserName = "";
        string m_senderPass = "";
        string m_smtpServer = "";

        public EmailSender(string sendermail,string serderusername,string senderpass,string smtpserver)
        {
            m_senderMail = sendermail;
            m_senderUserName = serderusername;
            m_senderPass = senderpass;
            m_smtpServer = smtpserver;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="ToEmaiAddress">收件人地址,字符串数组,多个地址</param>
        /// <param name="ToUserName">收件人姓名</param>
        /// <param name="ToSubject">邮件主题</param>
        /// <param name="ToBody">邮件内容</param>
        public void Send(System.Collections.Generic.List<string> ToEmaiAddress, string ToSubject, string ToBody)
        {
            //使用这个类之前,要先设置一个它的属性设置邮件服务器,若不设置的话将以你系统默认的邮件服务器来发送 
            SmtpClient smtpserver = new SmtpClient();
            smtpserver.Host = m_smtpServer;
            smtpserver.Credentials = new System.Net.NetworkCredential(m_senderUserName, m_senderPass);
            smtpserver.DeliveryMethod = SmtpDeliveryMethod.Network;

            //设定邮件主题
            string mailSubject = ToSubject;

            //设定邮件正文
            string mailBody = ToBody;

            //收件人地址,Encoding.BigEndianUnicode必须指定下,不然不识别中文,另外不能出现[]这样的字符


            //设置
            MailMessage mailMss = new MailMessage(); //创建对象 
            mailMss.From = new MailAddress(m_senderMail);//发件人地址,

            //mailMss.ReplyTo = mailReplayTo;//回复地址；
            mailMss.Sender = mailMss.From;//发信人地址, 是MailAddress类型 发件人与发信人不同，发件人是要在邮箱上显示的

            foreach (string toemail in ToEmaiAddress)
            {
                MailAddress mailTo = new MailAddress(toemail);
                mailMss.To.Add(mailTo);//收件人地址,是MailAddressCollection类型 
            }

            // mailMss.CC.Add(mailCc);//抄送邮件的地址,是MailAddressCollection类型
            mailMss.Subject = mailSubject;  //邮件主题,是字符串类型 
            mailMss.Body = mailBody;//邮件内容,是字符串类型 
            mailMss.Priority = MailPriority.High;//设置邮件的优先级 高优先级;MailPriority.Low低优先级 ;MailPriority.Normal一般优先级 
            //发送邮件 

            smtpserver.Send(mailMss);

        }

        public string SenerMail
        {
            get { return m_senderMail;}
            set { m_senderMail = value;}
        }

        public string SenderUserName
        {
            get { return m_senderUserName;}
            set { m_senderUserName = value;}
        }

        public string SenderPass
        {
            get { return m_senderPass;}
            set { m_senderPass = value;}
        }

        public string SmtpServer
        {
            get { return m_smtpServer;}
            set { m_smtpServer = value;}
        }

        
    }
}
